package com.uenpay.im.ui.old.activity.red;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.uenpay.im.R;

import java.util.ArrayList;

public class RedAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private ArrayList<RedEntity> arrayList = new ArrayList<RedEntity>();
	private int type;
	private Context mContext;

	public RedAdapter(Context _Context, ArrayList<RedEntity> _ArrayList,
			int _type) {
		mContext = _Context;
		mInflater = LayoutInflater.from(_Context);
		arrayList = _ArrayList;
		type = _type;
	}

	public void addData(ArrayList<RedEntity> _ArrayList) {
		arrayList.addAll(_ArrayList);
		notifyDataSetChanged();
	}

	public void setData(ArrayList<RedEntity> _ArrayList) {
		arrayList = _ArrayList;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return arrayList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return arrayList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		RedViewHolder holder = null;
		RedEntity redEntity = arrayList.get(arg0);
		if (arg1 == null) {
			holder = new RedViewHolder();
			arg1 = mInflater.inflate(R.layout.item_red_layout, null);
			holder.moneyTv = (TextView) arg1.findViewById(R.id.red_money_tv);
			holder.typeTv = (TextView) arg1.findViewById(R.id.red_type_tv);
			holder.dateTv = (TextView) arg1.findViewById(R.id.red_date_tv);
			holder.overTv = (TextView) arg1.findViewById(R.id.red_over_tv);
			arg1.setTag(holder);
		} else {
			holder = (RedViewHolder) arg1.getTag();
		}
		holder.moneyTv.setText(redEntity.amount+mContext.getString(R.string.red_element));
		if (type == 1) {
			holder.typeTv.setText(redEntity.fnickname);
		} else {
			if (redEntity.type == 2) {
				holder.typeTv.setText(mContext.getResources().getString(
						R.string.red_type_ordinary));
			} else {
				holder.typeTv.setText(mContext.getResources().getString(
						R.string.red_type_luck));
			}
		}
		holder.dateTv.setText(redEntity.createtime);
		if (type == 2) {
			holder.overTv
					.setText(redEntity.opennumber + "/" + redEntity.number);
		}

		return arg1;
	}

	private class RedViewHolder {
		private TextView typeTv, dateTv, moneyTv, overTv;
	}

}
